import React, {Component, Fragment} from 'react';
import './index.less';
import {base, promptBox, high} from 'nc-lightapp-front';
import setButtonStatus from "../../container/functions/setButtonStatus";
import saveBlacklistInfo from "../AddNew/functions/saveBlacklistInfo";
import templatePrint from "../../container/functions/templatePrint";
import ReferSearch from "src/hrpub/common/components/referSearch/org";
import printOutput from "../../container/functions/printOutput";
import setQueryMeta from "../../container/functions/setQueryMeta";
import checkPsnInfo from "../../container/functions/checkPsnInfo";
import NCBackBtn from "src/hrpub/common/components/hr-back";

const {NCCheckbox} = base;
const {PrintOutput} = high;

class Header extends Component {
    constructor(props) {
        super(props);
        this.state = {printData: {}};
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
        this.goBack = this.goBack.bind(this);
        this.pageQueryClick = this.pageQueryClick.bind(this);
        this.setPrePage = this.setPrePage.bind(this);
        this.previousPage = '';
    }

    setPrePage(value) {
        this.previousPage = value;
    }

    updateOrgValue(value) {
        this.props.updateOrgValue(value);
    }

    onChangeHandle(type) {
        return (value) => {
            this.props.updateState({
                [type]: value
            }, () => {
                if (this.props.hasSearched) {
                    this.props.getMainTableData();
                }
            });
        }
    }

    onBtnOperation(props, btncode, areacode, opt) {
        switch (btncode) {
            case 'add':
                this.previousPage = this.props.page;
                this.props.updateState({
                    page: 'add'
                }, () => {
                    setButtonStatus.call(this, 'add');
                    this.props.initCardForm();
                });
                break;
            case 'query':
                setQueryMeta.call(this);
                this.props.search.openAdvSearch('blackquery', true);
                break;
            case 'refresh':
                if (this.props.hasSearched) {
                    this.props.updateState({
                        pageInfo: {
                            ...this.props.pageInfo,
                            pageIndex: 1
                        }
                    }, () => {
                        this.props.getMainTableData();
                    });
                }
                break;
            case 'listrpt':
                this.props.updateState({
                    rosterModalVisible: true
                });
                break;
            case 'cancel':
                promptBox(
                    {
                        color: "warning",
                        title: this.props.json['hi6007-000008'], /* 国际化处理： 提示*/
                        content: this.props.json['hi6007-000009'], /* 国际化处理： 是否确认要取消？*/
                        beSureBtnClick: function () {
                            opt.props.updateState({
                                page: opt.previousPage
                            }, () => {
                                setButtonStatus.call(opt, opt.previousPage);
                                if (opt.previousPage !== 'main') {
                                    opt.props.initCardForm();
                                }
                            });
                        }
                    });
                break;
            case 'save':
                checkPsnInfo.call(this, this.props, () => {
                    saveBlacklistInfo.call(this);
                });
                break;
            case 'check':
                checkPsnInfo.call(this, this.props, () => {
                    promptBox(
                        {
                            color: "warning",
                            title: this.props.json['hi6007-000008'], /* 国际化处理： 提示*/
                            content: this.props.json['hi6007-000010'], /* 国际化处理： 解除后数据将不可恢复,要继续吗？*/
                            beSureBtnClick: function () {
                                opt.props.updateState({
                                    checkModalVisible: true
                                })
                            }
                        });
                });
                break;
            case 'delete':
                checkPsnInfo.call(this, this.props, () => {
                    this.props.updateState({
                        deleteConfirmModalVisible: true
                    });
                });
                break;
            case 'edit':
                checkPsnInfo.call(this, this.props, () => {
                    this.previousPage = this.props.page;
                    this.props.updateState({
                        page: 'edit'
                    }, () => {
                        setButtonStatus.call(this, 'edit');
                        this.props.initCardForm();
                    });
                });
                break;
            case 'print':
                templatePrint.call(this);
                break;
            case 'output':
                printOutput.call(this);
                break;
            default:
                break;
        }
    }

    goBack() {
        this.props.updateState({
            page: 'main'
        }, () => {
            setButtonStatus.call(this, 'main');
            this.props.getMainTableData();
        });
    }

    pageQueryClick(props, pk_psndoc_bad) {
        this.props.updateState({
            pk_psndoc_bad
        }, () => {
            this.props.initCardForm();
        });
    }

    render() {
        const {page, button, includeCheckedPsn, orgVal, nodetype, cardPagination} = this.props;
        const {createCardPagination} = cardPagination;
        return (
            <div className="header">
                {page === 'main' ? <Fragment>
                    {
                        nodetype === 'ORG_NODE' ?
                            <div className="header-refer">
                                <ReferSearch
                                    orgVal={orgVal}
                                    getOrgData={this.updateOrgValue}
                                />
                            </div> : null
                    }
                    <div className="header-checkbox-group">
                        <NCCheckbox
                            checked={includeCheckedPsn}
                            className="header-checkbox"
                            color="primary"
                            onChange={this.onChangeHandle('includeCheckedPsn')}
                        >
                            {this.props.json['hi6007-000011']}{/* 国际化处理： 包含已解除的人员*/}
                        </NCCheckbox>
                    </div>
                </Fragment> : null}

                {page === "card" ? <NCBackBtn onClick={this.goBack}/> : null}{/* 国际化处理： 返回*/}

                <div className="btn-group">
                    <div className="definition-icons">
                        {button.createButtonApp({
                            area: 'head',
                            onButtonClick: (props, btncode) => {
                                this.onBtnOperation(props, btncode, 'head', this);
                            }
                        })}
                    </div>
                </div>
                {page === "card" ? createCardPagination({handlePageInfoChange: this.pageQueryClick}) : null}
                <PrintOutput
                    ref='printOutput'
                    url={'/nccloud/hrhi/blacklist/BlackListPrintAction.do'}
                    data={this.state.printData}
                />
            </div>
        );
    }
}

export default Header;
